package com.shujia.spark.sql;

import java.sql.*;

public class Demo11SparkJDbc {

    public static void main(String[] args) throws Exception {

        //1、加载驱动

        Class.forName("org.apache.hive.jdbc.HiveDriver");

        //2，建立链接
        Connection con = DriverManager.getConnection("jdbc:hive2://master:10000");

        Statement stat2 = con.createStatement();
        //设置默认分区数据
        stat2.execute("set spark.sql.shuffle.partitions=2");

        //3、查询数据
        PreparedStatement stat = con.prepareStatement("select clazz,count(1) as c  from  student group by clazz");

        //4、执行查询
        ResultSet resultSet = stat.executeQuery();

        while (resultSet.next()) {
            String clazz = resultSet.getString("clazz");
            long c = resultSet.getLong("c");
            System.out.println(clazz + "\t" + c);
        }

        //5、关闭链接
        con.close();

    }
}
